﻿@inject IJSRuntime JS
@inject NavigationManager NavigationManager


<div class="@($"markdown-body {Class}")" Style="@Style">
    @((MarkupString)Text)
</div>

@code {
    [Parameter]
    public string Text { get; set; }
    [Parameter]
    public string Class { get; set; }
    [Parameter]
    public string Style { get; set; }
    [Parameter]
    public bool IsNeedHighlight { get; set; } = true;

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            if (IsNeedHighlight)
            {

                try
                {
                    await JS.InvokeAsync<string>("highlightAllCode");
                }
                catch
                {

                }
            }
        }
    }
    protected override async Task OnInitializedAsync()
    {

        if (string.IsNullOrWhiteSpace(Text) == false)
        {
            var url = NavigationManager.Uri.Replace(NavigationManager.BaseUri, "");
            Text = Text.Replace("href=\"#", $"href=\"{url}#").Replace("<img ", "<img loading=\"lazy\" ").Replace("<a href=\"http", "<a target=\"_blank\" href=\"http");
        }
    }

}
